package cn.hzking.gjmall.delegates.my;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fingdo.statelayout.StateLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import cn.hzking.gjmall.R;
import cn.hzking.gjmall.activitys.MainActivity;
import cn.hzking.gjmall.adpters.coupon.MyCouponAdapter;
import cn.hzking.gjmall.adpters.order.OrderAdapter;
import cn.hzking.gjmall.base.BaseSecondDelegate;
import cn.hzking.gjmall.bean.MyCouponBean;
import cn.hzking.gjmall.bean.OrderBean;
import cn.hzking.gjmall.net.RetrofitFactory;
import cn.hzking.gjmall.net.base.BaseEntity;
import cn.hzking.gjmall.net.base.BaseObserver;
import cn.hzking.gjmall.net.base.GjCustomStateViewObserver;
import cn.hzking.gjmall.utils.UIUtils;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.ResponseBody;

/**
 *
 */
public class MyCouponDelegate extends BaseSecondDelegate {
    public static final int UNUSED = 1;//未使用
    public static final int USED = 2;//已使用
    public static final int OVERTIME = 3;//已过期
    public static final String FRAGMENTID = "fragmentId";
    @Bind(R.id.recyclerview) RecyclerView recyclerview;
    @Bind(R.id.refrsh) SwipeRefreshLayout refrsh;
    @Bind(R.id.statlayout) StateLayout sl;
    private int fragmentId;
    private int pageIndex=1;
    private String couponType="";
    private MyCouponAdapter adapter;
    public MyCouponDelegate() {
    }

    public static MyCouponDelegate newInstance(int param1) {
        MyCouponDelegate fragment = new MyCouponDelegate();
        Bundle args = new Bundle();
        args.putInt(FRAGMENTID, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            fragmentId = getArguments().getInt(FRAGMENTID);
        }
        switch (fragmentId){
            case UNUSED:
                couponType="unusedCoupon";
                break;
            case USED:
                couponType="usedCoupon";
                break;
            case OVERTIME:
                couponType="expireCoupon";
                break;
            default:
                break;
        }
    }

    @Override
    public Object setLayout() {
        return R.layout.fragment_mycoupon;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(_mActivity);
        recyclerview.setLayoutManager(linearLayoutManager);
        adapter = new MyCouponAdapter(R.layout.item_coupon, null);
        adapter.setCouponType(couponType);
        recyclerview.setAdapter(adapter);
        refrsh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                getNetData(false,true,false,true,null,null);
            }
        },recyclerview);
        recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                int topRowVerticalPosition = (recyclerView == null || recyclerView.getChildCount() == 0) ? 0 : recyclerView.getChildAt(0).getTop();
                refrsh.setEnabled(topRowVerticalPosition >= 0);
            }
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }
        });
    }

    private void refresh() {
        pageIndex=1;
        getNetData(false,true,false,true,null,null);
    }



    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        getNetData(true,false,true,true,null,null);
    }

    public void getNetData(boolean isFristInit, boolean isRefrsh, boolean isShowDialog, boolean isCancel, View onNetView, View errorView) {
        HashMap<String, String> parms = new HashMap<>();
        parms.put("couponType",couponType);
        parms.put("pageIndex",""+pageIndex);
        RetrofitFactory.getInstant().API()
                .myCoupon(parms)
                .compose(this.<MyCouponBean>setThread())
                .subscribe(new GjCustomStateViewObserver<MyCouponBean>(_mActivity,sl,refrsh,isFristInit,isRefrsh,isShowDialog,isCancel,null,onNetView,errorView) {
                    @Override
                    protected void onBusinessSuccess(MyCouponBean myCouponBean) throws Exception {
                        if(myCouponBean!=null){
                            List<MyCouponBean.DataBean.CouponDetailQueryBean.ModuleBean> list=myCouponBean.getData().getCouponDetailQuery().getModule();
                            if(pageIndex==1){
                                adapter.setNewData(list);
                                if(adapter.getData().size() == 0){
                                 /*   View view = View.inflate(_mActivity,R.layout.layout_statelayout_custom_order_empty,null);
                                    sl.showCustomView(view);*/
                                    return;
                                }
                            }else {
                                adapter.addData(list);
                            }
                            pageIndex++;
                        }
                        if (myCouponBean.getData().getCouponDetailQuery().getTotalRecord() == adapter.getData().size()) {//数据加载完成
                            adapter.loadMoreEnd(false);
                        } else {
                            adapter.loadMoreComplete();
                        }
                    }

                    @Override
                    protected void onChildFailure(Throwable e, boolean isNetWorkError) throws Exception {

                    }

                    @Override
                    protected void onBusinessFail(MyCouponBean orderBean) {
                        super.onBusinessFail(orderBean);
                        UIUtils.showToastShort(_mActivity,orderBean.getErrorMsg());
                    }
                });
    }
}
